package week03;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class Week03Main {



    public static void main(String[] args) {

        String tableNameStudent = "demo2022_student";

        try {

            Configuration configuration = HBaseConfiguration.create();
            Connection connection = ConnectionFactory.createConnection(configuration);
            Admin admin = connection.getAdmin();

            // create table
            TableName tableName = TableName.valueOf(tableNameStudent);
            HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);

            HColumnDescriptor columnName = new HColumnDescriptor("name");
            HColumnDescriptor columnInfo = new HColumnDescriptor("info");
            HColumnDescriptor columnScore = new HColumnDescriptor("score");

            tableDescriptor.addFamily(columnName);
            tableDescriptor.addFamily(columnInfo);
            tableDescriptor.addFamily(columnScore);

            if(admin.tableExists(tableName)) {
                admin.disableTable(tableName);
                admin.deleteTable(tableName);
                System.out.println("HBase delete table:" + tableNameStudent);
            }

            admin.createTable(tableDescriptor);


            // add data
            Table table = connection.getTable(tableName);
            Put put1 = new Put("work202203_0001".getBytes());
            put1.addColumn("name".getBytes(), "name".getBytes(), "Tom".getBytes());
            put1.addColumn("info".getBytes(), "student_id".getBytes(), "20210000000001".getBytes());
            put1.addColumn("info".getBytes(), "class".getBytes(), "1".getBytes());
            put1.addColumn("score".getBytes(), "understanding".getBytes(), "75".getBytes());
            put1.addColumn("score".getBytes(), "programming".getBytes(), "82".getBytes());
            table.put(put1);
            System.out.println("put data 1");

            // add data
            List<Student> studentList = new ArrayList<Student>();
            studentList.add(new Student("list202203_1001", "Jerry", "20210000000002", "1", "85", "67"));
            studentList.add(new Student("list202203_1002", "Jack", "20210000000003", "2", "80", "80"));
            studentList.add(new Student("list202203_1003", "Rose", "20210000000004", "2", "60", "61"));
            for(Student student : studentList) {
                Put putStudent = new Put(student.getId().getBytes());
                putStudent.addColumn("name".getBytes(), "name".getBytes(), student.getName().getBytes());
                putStudent.addColumn("info".getBytes(), "student_id".getBytes(), student.getStudentId().getBytes());
                putStudent.addColumn("info".getBytes(), "class".getBytes(), student.getClassNumber().getBytes());
                putStudent.addColumn("score".getBytes(), "understanding".getBytes(), student.getUnderstanding().getBytes());
                putStudent.addColumn("score".getBytes(), "programming".getBytes(), student.getProgramming().getBytes());
                table.put(putStudent);
            }

            // get data
            Get get = new Get("work202203_0001".getBytes());
            Result result1 = table.get(get);
            System.out.println("get data:" + result1);

            // scan data
            Scan scan = new Scan();
            scan.addColumn("name".getBytes(), "name".getBytes());
            ResultScanner scanner = table.getScanner(scan);
            Iterator<Result> resultIterator = scanner.iterator();
            for(;resultIterator.hasNext();) {
                Result resultDemo = resultIterator.next();
                System.out.println("resultDemo: " + resultDemo);
                System.out.println(" row:" + new String(resultDemo.getRow())
                        + " value=" + new String(resultDemo.getValue("name".getBytes(), "name".getBytes())));
            }

            // delete
            Delete delete = new Delete("work202203_0001".getBytes());
            table.delete(delete);


            // close
            admin.close();
            connection.close();






        } catch(Exception e) {
            e.printStackTrace();
        }

    }
}
